﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Nette
{
    /// <summary>
    /// KVLine.xaml 的交互逻辑
    /// </summary>
    public partial class KVLine : UserControl
    {
        public event RoutedEventHandler DeleteClick;
        public event RoutedEventHandler TLostFocus;

        private bool _isEditable = true;
        public bool IsEditable{
            get => _isEditable;
            set {
                _isEditable = value;
                //设置不可编辑
                this.IDelBtn.IsEnabled = value;
                this.IKey.IsEnabled = value;
                this.IVal.IsEnabled = value;
                this.IEnable.IsEnabled = value;
            }
        }

        public bool? IsSelected
        {
            get => this.IEnable.IsChecked;
            set => this.IEnable.IsChecked = value;
        }
        /// <summary>
        /// 是否可以删掉
        /// </summary>
        public bool IsCanDelete
        {
            get => this.IDelBtn.IsEnabled;
            set => this.IDelBtn.IsEnabled = value;
        }

        public string KeyStr
        {
            get => this.IKey.Text;
            set => this.IKey.Text = value;
        }
        public string ValueStr
        {
            get => this.IVal.Text;
            set => this.IVal.Text = value;
        }
        public Button DeleteButton
        {
            get => this.IDelBtn;
        }

        public KVLine(string keyStr, string valueStr, bool isEditable, bool isSelected) : this(keyStr,valueStr,isEditable)
        {
            this.IsSelected = isSelected;
        }
        public KVLine(string keyStr, string valueStr, bool isEditable) : this()
        {
            KeyStr = keyStr;
            ValueStr = valueStr;
            IsEditable = isEditable;
        }

        public KVLine()
        {
            InitializeComponent();
            this.IDelBtn.Click += onDelClick;
            this.IKey.LostKeyboardFocus += onLostFocus;
            this.IVal.LostKeyboardFocus += onLostFocus;
        }
        ~KVLine()
        {
            this.IDelBtn.Click -= onDelClick;
            this.IKey.LostKeyboardFocus -= onLostFocus;
            this.IVal.LostKeyboardFocus -= onLostFocus;
        }

        private void onDelClick(object sender, RoutedEventArgs e)
        {
            DeleteClick(this, e);
        }
        private void onLostFocus(object sender, RoutedEventArgs e)
        {
            TLostFocus(this, e);
        }
    }
}
